package com.ficus.road.maintain.third.dk;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.ficus.road.maintain.dao.xlj.XljCameraGroupDao;
import com.google.common.base.Objects;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * @author xiawen yang
 * @date 2022/7/12 下午3:43
 */
@Component
@Slf4j
public class DkCameraGroup extends BaseDK {
    private static final String REQ_TYPE = "get_camera_group_request";

    @Autowired
    private XljCameraGroupDao xljCameraGroupDao;

    @Override
    protected String getReqType() {
        return REQ_TYPE;
    }

    @Scheduled(cron = "0 0 14 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void refreshCameraGroup() {
        Optional<DKCameraGroupHttpResponse> optional = getDKData(DKCameraGroupHttpResponse.class);
        if (optional.isEmpty() || Objects.equal(optional.get().getRetHeader().getCode(), 500)) {
            return;
        }
        optional.get().getRetBody().getCameraGroupList().forEach(item -> {
            xljCameraGroupDao.saveOrUpdate(item);
        });
    }

    @Data
    public static class XljCameraGroup {

        @Id
        @JsonProperty("group_id")
        private int groupId;
        @JsonProperty("group_name")
        private String groupName;
        @JsonProperty("parent_group_id")
        private int parentGroupId;
    }

    @Data
    public static class XljCameraGroupRetBody {
        @JsonProperty("camera_group_list")
        private List<XljCameraGroup> cameraGroupList;
    }

    @Data
    public static class DKCameraGroupHttpResponse extends DKBasicHttpResponse {
        @JsonProperty("ret_body")
        private XljCameraGroupRetBody retBody;
    }
}
